﻿using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Kargo
{
    public partial class KargoKoli : XPopupForm
    {
        int kargoId;
        public KargoKoli()
        {
            InitializeComponent();
        }

        public KargoKoli(int kargoId)
            : this()
        {
            this.kargoId = kargoId;
        }

        decimal kargoFiyat = 0.0M;
        DataTable dZ, dF;
        private void KargoKoli_Load(object sender, EventArgs e)
        {

            FooterText = "KargoFiyatı = NakliyeÜcreti + %10 Marj + 10€ ATR";

            calcBoy.Enabled = calcEn.Enabled = calcYukseklik.Enabled = calcAgirlik.Enabled = false;

            DataSet d = ExecuteDataSet("GetKargoFiyat");
            dZ = d.Tables[0];
            dF = d.Tables[1];
            RepositoryItemComboBox riPackage = new RepositoryItemComboBox();
            riPackage.Items.AddRange(new object[] { "Koli1", "Koli2", "Koli3", "Koli4", "Koli5", "Koli6" });
            riPackage.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            cmbPackage.Properties.Assign(riPackage);
            cmbPackage.SelectedIndexChanged += cmbPackage_SelectedIndexChanged;

            RepositoryItemGridLookUpEdit riCountry = RI(Rp.Country) as RepositoryItemGridLookUpEdit;
            riCountry.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gleCountry.Properties.Assign(riCountry);

            gleCountry.EditValueChanged += gleCountry_EditValueChanged;

            chkManDimensions.CheckedChanged += chkManDimensions_CheckedChanged;

            calcAgirlik.EditValueChanged += calcAgirlik_EditValueChanged;
            calcBoy.EditValueChanged += calcBoy_EditValueChanged;
            calcEn.EditValueChanged += calcEn_EditValueChanged;
            calcYukseklik.EditValueChanged += calcYukseklik_EditValueChanged;
        }

        void calcYukseklik_EditValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }

        void calcEn_EditValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }

        void calcBoy_EditValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }

        void calcAgirlik_EditValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }

        void chkManDimensions_CheckedChanged(object sender, EventArgs e)
        {
            if (chkManDimensions.Checked)
            {
                calcBoy.Enabled = calcEn.Enabled = calcYukseklik.Enabled = calcAgirlik.Enabled = true;
                cmbPackage.Enabled = false;
            }
            else
            {
                calcBoy.Enabled = calcEn.Enabled = calcYukseklik.Enabled = calcAgirlik.Enabled = false;
                cmbPackage.Enabled = true;
                cmbPackage.SelectedIndex = 0;
            }
        }

        void cmbPackage_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbPackage.SelectedIndex)
            {
                case 0:
                    calcBoy.Value = 30.5M;
                    calcEn.Value = 46.5M;
                    calcYukseklik.Value = 16;
                    break;

                case 1:
                    calcBoy.Value = 31.5M;
                    calcEn.Value = 46.7M;
                    calcYukseklik.Value = 25.5M;
                    break;


                case 2:
                    calcBoy.Value = 30.5M;
                    calcEn.Value = 45.5M;
                    calcYukseklik.Value = 41.5M;
                    break;


                case 3:
                    calcBoy.Value = 42.5M;
                    calcEn.Value = 42.5M;
                    calcYukseklik.Value = 42.5M;
                    break;


                case 4:
                    calcBoy.Value = 40.0M;
                    calcEn.Value = 59.5M;
                    calcYukseklik.Value = 39.0M;
                    break;


                case 5:
                    calcBoy.Value = 50.0M;
                    calcEn.Value = 70.5M;
                    calcYukseklik.Value = 40.0M;
                    break;

                default:
                    break;
            }
            FiyatHesapla();
        }

        decimal calculatedWeight;
        private void FiyatHesapla()
        {
            calculatedWeight = (decimal)(calcBoy.Value * calcEn.Value * calcYukseklik.Value) / 6000 * numOfPackages.Value.ToInt();

            if (calculatedWeight > 10.0M)     //10'a kadar buçuklu, sonrasında 1'er
                calculatedWeight = Math.Ceiling(calculatedWeight);
            else
                calculatedWeight = Math.Ceiling(calculatedWeight / 0.5M) * 0.5M;

            lblAgirlik.Text = calculatedWeight.ToStr() + " kg.";

            if (gleCountry.EditValue.ToInt() == 0)
            {
                kargoFiyat = 0.0M;
                //XtraMessageBox.Show("Lütfen fiyat hesaplanacak ülkeyi seçiniz.");
            }
            else
            {
                decimal realWeight = calcAgirlik.Value.ToDecimal();
                if (calculatedWeight > realWeight)
                    realWeight = calculatedWeight;

                bool ozelFiyat = @"Almanya
Avusturya
Belçika
Danimarka
Fransa
Hollanda
İrlanda
İngiltere
İspanya
İsviçre
İtalya
Luksemburg
Norveç
Portekiz
Yunanistan".Contains(gleCountry.Text) && realWeight >= 30;

                lblOzelFiyat.Visible = ozelFiyat;

                if (ozelFiyat)
                    kargoFiyat = realWeight * 2;
                else if (realWeight == 0.0M)
                    kargoFiyat = 0.0M;
                else
                {
                    int countryId = gleCountry.EditValue.ToInt(-1);
                    if (dZ.Select("CountryId = " + countryId.ToStr()).Length > 0)
                    {
                        string Zone = "Zone" + dZ.Select("CountryId = " + countryId.ToStr())[0]["Zone"].ToStr();
                        if (realWeight > 30)
                        {
                            decimal asim = dF.Select("Agirlik=-1")[0][Zone].ToDecimal();
                            decimal Fiyat30 = dF.Select("Agirlik=30")[0][Zone].ToDecimal();
                            kargoFiyat = Fiyat30 + (realWeight - 30) * asim;
                        }
                        else
                            kargoFiyat = dF.Select("Agirlik=" + realWeight.ToStr().Replace(",", "."))[0][Zone].ToDecimal();
                    }


                    //kargoFiyat = ExecuteScalar("GetKargoFiyat", countryId, realWeight).ToDecimal();
                }
            }
            lblKargoFiyat.Text = "Kargo Ücreti: " + (Math.Round(kargoFiyat * 1.1M + 10, 2)).ToStr() + " €"; // Cemal Bey: %10 Marj ekliyoruz.
        }

        void gleCountry_EditValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }

        private void numOfPackages_ValueChanged(object sender, EventArgs e)
        {
            FiyatHesapla();
        }
    }
}